<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/8/6
 * Time: 9:32
 */

namespace app\api\model;


use think\Model;

class Classroom extends Model
{
    /**
     * 课堂列表
     * @param $field  需要查询的字段
     * @param $where  查询条件
     * @param $order  排序条件
     * @param $page   分页  当前页码
     * @param $size   每页显示的记录数
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getClasssroomList($field, $where, $order, $page, $size){
        $pageNum = $page - 1;
        $list = $this->field($field)
            ->where($where)
            ->where('classroom_status', 2)
            ->order($order)
            ->limit($pageNum * $size, $size)
            ->select();
        return $list;
    }
    
    /**
     * 查询某课程的详情
     * @param $field  查询字段
     * @param $classroom_id  课程ID
     * @return array|false|\PDOStatement|string|Model
     */
    public function getClassroomInfo($field, $classroom_id){
        $classroomInfo = $this->field($field)
            ->where('id', $classroom_id)
            ->find();
        return $classroomInfo;
    }
    
    
    
    /**
     * 收藏改变排序字段
     * @param $type  收藏为1  取消收藏为2
     * @param $classroom_id  课程ID
     */
    public function collectSort($type, $classroom_id){
        if($type == 1){//收藏
            $this->where('id', $classroom_id)->setInc('classroom_sort');//排序字段加1
        }else{//取消收藏
            $this->where('id', $classroom_id)->setDec('classroom_sort');//排序字段减1
        }
    }
    
    /**
     * 发布评论改变排序字段
     * @param $classroom_id 课程ID
     */
    public function commentSort($type, $classroom_id, $num = ''){
        if($type == 1){//发布评论/回复
            $this->where('id', $classroom_id)->setInc('classroom_sort');//排序字段加1
        }else{//删除评论/回复
            $this->where('id', $classroom_id)->setDec('classroom_sort', $num);//排序字段减删除的评论/回复数
        }
    }
    
    /**
     * 播放量改变排序字段
     * @param $classroom_id 课程ID
     */
    public function playSort($classroom_id){
        $this->where('id', $classroom_id)->setInc('classroom_sort', 2);//排序字段加2
        $this->where('id', $classroom_id)->setInc('classroom_playnum');//播放量字段加1
    }
}